﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement;
using Orchard.Core.Common.Models;
using Orchard.Environment;
using Orchard.Environment.Extensions.Models;
using Orchard.Core.Navigation.Models;
using Orchard.Security;
using Orchard.Settings;
using Orchard.Widgets.Models;
using Orchard.Widgets.Services;
using SchoolPointe.Menus.Enums;
using SchoolPointe.Menus.Models;
using SchoolPointe.RelatedContent.Models;
using SchoolPointe.RelatedContent.Services;
using Orchard.ContentManagement.MetaData;

namespace SchoolPointe.Menus
{
    public class Events : IFeatureEventHandler
    {
        private readonly IContentManager _contentManager;
        private readonly IRelatedContentService _relatedContentService;
        private readonly IMembershipService _membershipService;
        private readonly ISiteService _siteService;
        private readonly IWidgetsService _widgetsService;
        private readonly IContentDefinitionManager _contentDefinitionManager;

        public Events(
            IContentManager contentManager, 
            IRelatedContentService relatedContentService, 
            IMembershipService membershipService, 
            ISiteService siteService, 
            IWidgetsService widgetsService,
            IContentDefinitionManager contentDefinitionManager)
        {
            _contentManager = contentManager;
            _relatedContentService = relatedContentService;
            _membershipService = membershipService;
            _siteService = siteService;
            _widgetsService = widgetsService;
            _contentDefinitionManager = contentDefinitionManager;
        }

        public void Installing(Feature feature)
        {            
        }

        public void Installed(Feature feature)
        {
        }

        public void Enabling(Feature feature)
        {
        }

        public void Enabled(Feature feature)
        {
            if (feature.Descriptor.Id == "SchoolPointe.Menus")
            {
                //create menu for "main menu"
                if (_contentManager.Query("Menu").Count() == 0)
                {
                    var menu = _contentManager.Create<BetterMenuPart>("Menu", p => p.Name = "Main Menu");
                    var owner = _membershipService.GetUser(_siteService.GetSiteSettings().SuperUser);
                    menu.As<CommonPart>().Owner = owner;

                    foreach (var menuItem in _contentManager.Query<MenuItemPart, MenuItemPartRecord>("MenuItem").List())
                    {
                        menuItem.As<BetterMenuItemPart>().MenuItemType = MenuItemType.CustomLink;
                        menuItem.As<BetterMenuItemPart>().Url = menuItem.Url;
                        menuItem.As<BetterMenuItemPart>().Text = menuItem.As<MenuPart>().MenuText;
                        _relatedContentService.SetRelatedItems(menuItem.As<RelatedContentPart>(), "menuitem-parent", new List<int> { menu.Id });
                    }

                    // Adding the default menu widget to Navigation zone in Default layer
                    var layerPart = _widgetsService.GetLayers().FirstOrDefault(l => l.Name == "Default");
                    if (layerPart == null)
                    {
                        return;
                    }
                    var countDefault = _contentManager
                        .Query<WidgetPart, WidgetPartRecord>()
                        .Where(p => p.Zone == "Navigation")
                        .ForType("MenuWidget").List();
                    if (!countDefault.Any())
                    {
                        var menuWidget = _contentManager.Create<WidgetPart>("MenuWidget",
                                                                    widget =>
                                                                    {
                                                                        widget.Record.Title = "Main menu";
                                                                        widget.Record.Position = "0";
                                                                        widget.Record.Zone = "Navigation";
                                                                        widget.LayerPart = layerPart;
                                                                    });
                        _relatedContentService.SetRelatedItems(menuWidget.As<RelatedContentPart>(), "menu", new List<int> { menu.Id });
                    }
                }
            }
            else if (feature.Descriptor.Id == "SchoolPointe.Menus.Classy")
            {
                _contentDefinitionManager.AlterTypeDefinition("MenuItem", def =>
                {
                    def.WithPart("CustomCss");
                });
            }
        }

        public void Disabling(Feature feature)
        {
        }

        public void Disabled(Feature feature)
        {
            if (feature.Descriptor.Id == "SchoolPointe.Menus.Classy")
            {
                _contentDefinitionManager.AlterTypeDefinition("MenuItem", def =>
                {
                    def.RemovePart("CustomCss");
                });
            }
        }

        public void Uninstalling(Feature feature)
        {
        }

        public void Uninstalled(Feature feature)
        {
        }
    }
}